﻿using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using EpiInfoCloudMagpiBridge.Models;
using Ewav.Security;

namespace EpiInfoCloudMagpiBridge.Controllers
{
        [Authorize]
    public class MagpiAccountSettingsController : Controller
    {
            readonly Cryptography cy = new Cryptography();

            readonly MagpiEWAVEntities db;     


        public MagpiAccountSettingsController()
        {
            db = new MagpiEWAVEntities(cy.Decrypt(ConfigurationManager.ConnectionStrings["MagpiEWAVEntities"].ToString())); 
        }


        // GET: MagpiAccountSettings
        public ActionResult Index()
        {
            return View(db.MagpiAccountSettings.ToList());
        }

        // GET: MagpiAccountSettings/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MagpiAccountSetting magpiAccountSetting = db.MagpiAccountSettings.Find(id);
            if (magpiAccountSetting == null)
            {
                return HttpNotFound();
            }
            return View(magpiAccountSetting);
        }

        // GET: MagpiAccountSettings/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: MagpiAccountSettings/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,EWAVUserID,MagpiUsername,MagpiAccessToken")] MagpiAccountSetting magpiAccountSetting)
        {
            if (ModelState.IsValid)
            {
                magpiAccountSetting.EWAVUserID = ApplicationModel.CurrentEwavUserId;      
                db.MagpiAccountSettings.Add(magpiAccountSetting);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(magpiAccountSetting);
        }

        // GET: MagpiAccountSettings/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MagpiAccountSetting magpiAccountSetting = db.MagpiAccountSettings.Find(id);
            if (magpiAccountSetting == null)
            {
                return HttpNotFound();
            }
            return View(magpiAccountSetting);
        }

        // POST: MagpiAccountSettings/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,EWAVUserID,MagpiUsername,MagpiAccessToken")] MagpiAccountSetting magpiAccountSetting)
        {
            if (ModelState.IsValid)
            {
                magpiAccountSetting.EWAVUserID = ApplicationModel.CurrentEwavUserId;
                db.Entry(magpiAccountSetting).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(magpiAccountSetting);
        }

        // GET: MagpiAccountSettings/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MagpiAccountSetting magpiAccountSetting = db.MagpiAccountSettings.Find(id);
            if (magpiAccountSetting == null)
            {
                return HttpNotFound();
            }
            return View(magpiAccountSetting);
        }

        // POST: MagpiAccountSettings/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MagpiAccountSetting magpiAccountSetting = db.MagpiAccountSettings.Find(id);
            db.MagpiAccountSettings.Remove(magpiAccountSetting);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
